"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Compatibility
Case Name   : 普通行存表复合唯一键列有重复值，部分测试点合理报错
Create At   : 2023/06/02
Owner       : n@ningyali
Description :
        1.建表
        2.复合唯一键列有重复列值，使用replace
        3.复合唯一键列有重复列值，不使用replace
        4.清理环境
Expect      :
        1.创建成功
        2.成功
        3.合理报错
        4.成功
History     :
"""
import os
import unittest

from yat.test import Node

from testcase.COMPATIBILITY.GRAMMAR.LOAD. \
    Opengauss_Compatibility_Grammar_Load_Common import LoadData
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class LoadData0035(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.load_data = LoadData()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.case_lable = 'load_data_0035'
        self.db_name = f'db_{self.case_lable}'
        self.t_name = f't_{self.case_lable}'
        self.file_path = os.path.join('/home', self.pri_user.ssh_user)
        self.cols = 'c_id int not null ,c_name varchar(50),c_age int,' \
                    'c_cid char(50),unique (c_id,c_name,c_cid)'

        text1 = '----pre_step1：创建数据文件；expect:创建成功----'
        self.log.info(text1)
        self.data = r"#11/'aaa'/22/'测试'\$#11/'bbb'/33/'fawer'\$" \
                    r"#11/'ccc'/25/'художников'\$#11/'bbb'/23/'fawer'\$" \
                    r"#33/'ccc'/23/'fawer'\$#11/'aaa'/24/'プがやってきた'\$" \
                    r"#11/'aaa'/111/'プがやってきた'\$" \
                    r"#11/'aaa'/555/'プがやってきた'\$"
        res = self.load_data.create_data_file(self.pri_user, self.case_lable,
                                              self.data)
        self.assertTrue(res, '执行失败:' + text1)

        text2 = '----pre_step2：创建兼容B库；expect:创建成功----'
        self.log.info(text2)
        res = self.load_data.create_b_db(self.pri_sh, self.db_name)
        self.assertTrue(res, '执行失败:' + text2)

    def test_load(self):
        text = '----step1：建表；expect：建表成功----'
        self.log.info(text)
        res = self.load_data.create_table(self.pri_sh, self.db_name,
                                          self.t_name, self.cols)
        self.assertTrue(res, '执行失败:' + text)

        text = '----step2：复合唯一键列有重复列值，使用replace；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_1.txt')
        res1 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path, truncate=False,
                                        col_sep='/', line_start='#',
                                        line_end='$')
        rows, _ = self.common.parse_sql_query_res(res1)
        expect = [['11', 'aaa', '22', '测试'],
                  ['11', 'ccc', '25', 'художников'],
                  ['11', 'bbb', '23', 'fawer'],
                  ['33', 'ccc', '23', 'fawer'],
                  ['11', 'aaa', '555', 'プがやってきた']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '----step3：复合唯一键列有重复列值，不使用replace；expect：合理报错----'
        self.log.info(text)
        res2 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path, into_type='',
                                        col_sep='/', line_start='#',
                                        line_end='$')
        einfo = 'ERROR:  duplicate key value violates unique constraint'
        self.assertIn(einfo, res2, '执行失败' + text)

    def tearDown(self):
        self.log.info('----step4：清理环境；expect：清理成功----')
        res = self.load_data.clear_env(self.pri_sh, self.pri_user,
                                       self.db_name,
                                       os.path.join(self.file_path,
                                                    f"{self.case_lable}*"))
        self.assertTrue(res, '执行失败:清理环境')
        self.log.info(f'----{os.path.basename(__file__)} end----')
